import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/home/goods',
    component: Home,
    children: [
      {
        path: '/home/goods',
        name: 'goods',
        component: ()=> import('../views/home/Goods.vue')
      },
      {
        path: '/home/menu',
        name: 'menu',
        component: ()=> import('../views/home/Menu.vue')
      },
      {
        path: '/home/order',
        name: 'order',
        component: ()=> import('../views/home/Order.vue')
      },
      {
        path: '/home/top',
        name: 'top',
        component: ()=> import('../views/home/Top.vue')
      },
      {
        path: '/home/user',
        name: 'user',
        component: ()=> import('../views/home/User.vue')
      },
      {
        path: '/home/users',
        name: 'users',
        component: ()=> import('../views/home/Users.vue')
      },
      {
        path: '/home/round',
        name: 'round',
        component: ()=> import('../views/home/Round.vue')
      },
      {
        path: '/home/pay',
        name: 'pay',
        component: ()=> import('../views/home/Pay.vue')
      },
      {
        path: '/home/pays',
        name: 'pays',
        component: ()=> import('../views/home/Pays.vue')
      },
      {
        path: '/home/company',
        name: 'company',
        component: ()=> import('../views/home/Company.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

var isLogin = localStorage.user;
router.beforeEach((to, from, next)=> {
  if(to.path == '/login') {
    next()
  }else {
    if(isLogin) {
      next()
    }else {
      next('/login')
    }
  }
})

export default router
